package com.flea.hclv.util;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * @program: project
 * @description: mail
 * @author: shiliqiang
 * @create: 2018-12-10 13:41
 * @version: 1.0
 */
public class EmailUtil {
    private String form;

    private String formPassword;

    private String receive;

    private String title;

    private String content;

    EmailUtil(String form, String formPassword, String receive, String title, String content) {
        this.form = form;
        this.formPassword = formPassword;
        this.receive = receive;
        this.title = title;
        this.content = content;
    }

    public static EmailBuilder builder() {
        return new EmailBuilder();
    }

    /**
     * 21      * 使用Transport 非静态方法 发送邮件
     * 22      * 连接163服务，给QQ邮箱发邮件
     * 23
     */
    public void sendEmail() {
        // 属性
        Properties properties = new Properties();
        // 设置认证属性
        properties.setProperty("mail.smtp.auth", "true");
        // 设置通信协议
        properties.setProperty("mail.transport.protocol", "smtp");
        // 邮件环境信息
        Session session = Session.getInstance(properties);
        // 调试,打印信息
        session.setDebug(true);
        Message message = new MimeMessage(session);
        try {
            // 主题
            message.setSubject(title);
            // 发送人
            message.setFrom(new InternetAddress("15511892086@163.com"));
            // 内容
            message.setText(content);
            // 邮件传输对象
            Transport transport = session.getTransport();
            // 传输连接：host，port，user，pass/主机，端口，用户名，密码
            transport.connect("smtp.163.com", 25, "15511892086@163.com", "jia,1234");
            // 发送邮件
            transport.sendMessage(message, new Address[]{new InternetAddress(receive)});
            // 关闭连接
            transport.close();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}
